package com.qf.demo3.service.Impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.github.pagehelper.PageHelper;
import com.qf.demo3.Utils.AddExcel;
import com.qf.demo3.Utils.WriteExcel;
import com.qf.demo3.dao.DataDao;
import com.qf.demo3.entity.Address;
import com.qf.demo3.service.DataService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Service
public class DataServiceImpl implements DataService {

    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    private DataDao dataDao;

    @Override
    public void saveData() {
        List<Address> list = new ArrayList<Address>();

        for (int i = 1;i <= 1000;i++){
            list.add(new Address(i,"辽宁省","沈阳市","于洪区","北陵街道","元江街"+i+"号"));
        }

        for (Address address : list){
            System.out.println(address);
            System.out.println(dataDao.insert(address));
        }
    }

    @Override
    public void readData() throws InterruptedException {

        CountDownLatch test = new CountDownLatch(5);
        ExecutorService threadPool = Executors.newFixedThreadPool(10);

        final List<Address>[] addresses = new List[5];


        threadPool.execute(new Runnable() {
            @Override
            public void run() {
                System.out.println("线程1");
                PageHelper.startPage(1,200);
                addresses[0] = dataDao.select();
                System.out.println("线程1读取over");
                test.countDown();
            }
        });

        threadPool.execute(new Runnable() {
            @Override
            public void run() {
                System.out.println("线程2");
                PageHelper.startPage(2,200);
                addresses[1] = dataDao.select();
                System.out.println("线程2读取over");
                test.countDown();
            }
        });
        threadPool.execute(new Runnable() {
            @Override
            public void run() {
                System.out.println("线程3");
                PageHelper.startPage(3,200);
                addresses[2] = dataDao.select();
                System.out.println("线程3读取over");
                test.countDown();
            }
        });
        threadPool.execute(new Runnable() {
            @Override
            public void run() {
                System.out.println("线程4");
                PageHelper.startPage(4,200);
                addresses[3] = dataDao.select();
                System.out.println("线程4读取over");
                test.countDown();
            }
        });

        threadPool.execute(new Runnable() {
            @Override
            public void run() {
                System.out.println("线程5");
                PageHelper.startPage(5,200);
                addresses[4] = dataDao.select();
                System.out.println("线程4读取over");
                test.countDown();
            }
        });
        test.await();

        System.out.println("-----数据读取完毕，开始写入Excel-----");
        CountDownLatch ch = new CountDownLatch(5);
        for (int i = 0; i < addresses.length; i++){
            if (i == 0){
                CountDownLatch c = new CountDownLatch(1);
                int finalI = i;
                threadPool.execute(new Runnable() {
                    @SneakyThrows
                    @Override
                    public void run() {
                        WriteExcel.write(addresses[finalI],"D:\\桌面\\address1.xlsx");
                        ch.countDown();
                    }
                });
                c.countDown();
                c.await();

            }else {
                CountDownLatch b = new CountDownLatch(1);
                int finalI1 = i;
                threadPool.execute(new Runnable() {
                    @SneakyThrows
                    @Override
                    public void run() {
                        AddExcel.addNew(addresses[finalI1],"D:\\桌面\\address1.xlsx");
                        ch.countDown();
                    }
                });
                b.countDown();
                b.await();

            }
        }
        ch.await();
        threadPool.shutdown();
        System.out.println("Excel写入完毕！！");


    }
}
